package com.doradance.app.appactivity.message;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.doradance.R;
import com.doradance.messageDao.Message;
import com.nostra13.universalimageloader.utils.L;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/3/21.
 */

public class MessageListAdapter extends RecyclerViewBaseAdapter<Message> {

    public MessageListAdapter(Context con) {
        super(con);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new VH(setAdapterViewItem(parent, R.layout.message_item));
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder viewHolder, int position) {
        VH holder = (VH) viewHolder;
        Message pushMessageEntity = getData().get(position);
        String data = pushMessageEntity.getData();
        try {

            JSONObject o = new JSONObject(data);
            holder.mTvMeaasgeTitle.setText((String)o.get("title"));
            Long date1 = (Long) o.get("date");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(date1);
            holder.mTvMessageTime.setText(formatter.format(date));
            holder.mTvMeaasgeTitle.setText((String)o.get("title"));
            holder.mTvMessageContent.setText(Html.fromHtml((String)o.get("content")));
            if(pushMessageEntity.getType().equals(1+"")){
                holder.mIvRedCir.setVisibility(View.INVISIBLE);
                holder.mTvMeaasgeTitle.setTextColor(Color.parseColor("#A8A8A8"));
            }else{
                holder.mIvRedCir.setVisibility(View.VISIBLE);
                holder.mTvMeaasgeTitle.setTextColor(Color.parseColor("#1A191D"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public class VH extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_red_cir)
        ImageView mIvRedCir;
        @Bind(R.id.tv_meaasge_title)
        TextView mTvMeaasgeTitle;
        @Bind(R.id.tv_message_time)
        TextView mTvMessageTime;
        @Bind(R.id.tv_message_content)
        TextView mTvMessageContent;
        public VH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
